package com.orange.core.service.feedback.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.orange.core.service.feedback.FeedbackService;
import com.orange.core.mapper.feedback.FeedbackMapper;
import com.orange.core.pojo.PageReqDto;
import com.orange.core.pojo.entity.feedback.Feedback;
import com.orange.core.pojo.entity.feedback.dto.FeedbackDTO;
import com.orange.core.pojo.entity.feedback.vo.FeedbackVO;
import com.orange.core.pojo.req.feedback.FeedbackPageReq;
import com.orange.core.util.ConvertUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;

/**
* @author lwm
* @description 针对表【feedback(商户意见反馈表)】的数据库操作Service实现
* @createDate 2025-05-29 11:12:11
*/
@Service
public class FeedbackServiceImpl extends ServiceImpl<FeedbackMapper, Feedback>
    implements FeedbackService {

    @Override
    public Page<FeedbackVO> page(PageReqDto<FeedbackPageReq> pageReqDto) {
        if(pageReqDto == null){
            pageReqDto = new PageReqDto<>();
            pageReqDto.setCondition(new FeedbackPageReq());
        } else if (pageReqDto.getCondition() == null) {
            pageReqDto.setCondition(new FeedbackPageReq());
        }
        FeedbackPageReq condition = pageReqDto.getCondition();
        Page<Feedback> page = this.lambdaQuery()
                .eq(condition.getMerchantId() != null, Feedback::getMerchantId, condition.getMerchantId())
                .like(StrUtil.isNotBlank(condition.getTitle()), Feedback::getTitle, condition.getTitle())
                .page(pageReqDto.toPage());
        Page<FeedbackVO> feedbackVOPage = new Page<>(pageReqDto.getPage(), pageReqDto.getSize(), page.getTotal());
        List<FeedbackVO> feedbackVOList = ConvertUtils.convertListToVo(page.getRecords(), FeedbackVO.class);
        feedbackVOPage.setRecords(feedbackVOList);
        return feedbackVOPage;
    }

    @Override
    public FeedbackVO info(Integer id) {
        Feedback feedback = this.getById(id);
        Assert.notNull(feedback, "意见反馈不存在");
        return ConvertUtils.convertToVo(feedback, FeedbackVO.class);
    }

    @Override
    @Transactional
    public boolean save(FeedbackDTO feedbackDTO) {
        Feedback feedback = new Feedback();
        BeanUtil.copyProperties(feedbackDTO, feedback);
        boolean result = this.save(feedback);
        return result;
    }

    @Override
    @Transactional
    public boolean update(FeedbackDTO feedbackDTO) {
        Feedback feedback = new Feedback();
        BeanUtil.copyProperties(feedbackDTO, feedback);
        boolean result = this.updateById(feedback);
        return result;
    }

    @Override
    @Transactional
    public boolean deleteFeedbackByIds(Integer[] ids) {
        boolean result = this.removeBatchByIds(Arrays.asList(ids));
        return result;
    }


}




